package com.longg.controller;

import com.longg.comm.Comm;
import com.longg.model.Dept;
import com.longg.service.IDeptService;
import com.longg.service.IMeetingService;
import com.longg.service.impl.DeptServiceImpl;
import com.longg.service.impl.MeetingServiceImpl;
import com.longg.utils.PageUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/meeting/*")
public class MeetingController extends BaseController{
    private IMeetingService meetingService = new MeetingServiceImpl();
    private IDeptService deptService = new DeptServiceImpl();

    public void meetingList(HttpServletRequest req, HttpServletResponse resp){
        int currentPage = 1;
        HttpSession session = req.getSession();
        String page = req.getParameter("currentPage");
        String deptName = req.getParameter("dep");
        if(null != page && !"".equals(page)){
            currentPage = Integer.parseInt(page);
        }

        String name = req.getParameter("name");
        List<Map<String, Object>> meetingsByPage = meetingService.findMeetingsByPage(currentPage,name,deptName);
        List<Dept> deptList = deptService.findAll();
        long count = meetingService.getCount(name,deptName);
        long totalPage = PageUtil.getTotalPage(count);
        long startPage = PageUtil.getStartPage(currentPage);
        long endPage = PageUtil.getEndPage(currentPage,count);
        session.setAttribute("currentPage",currentPage);
        session.setAttribute("count",count);
        session.setAttribute("totalPage",totalPage);
        session.setAttribute("startPage",startPage);
        session.setAttribute("endPage",endPage);
        session.setAttribute("meetingsByPage",meetingsByPage);
        session.setAttribute("pageCount", Comm.PAGE_COUNT);
        session.setAttribute("name",name);
        session.setAttribute("deptList",deptList);
        try {
            resp.sendRedirect(req.getContextPath() + "/assets/html/meeting.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addMeeting(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        String title = req.getParameter("title");
        String deptName = req.getParameter("dep");
        String startTime = req.getParameter("startTime");
        String content = req.getParameter("content");
        String startDate = req.getParameter("startDate");
        int uid = getUserId(req);
        try {
            if(null == title || "".equals(title) || null == deptName || "".equals(deptName)
                    || null == startTime || "".equals(startTime) || null == content || "".equals(content)){
                session.setAttribute("meetMsg","参数不能为空");
                resp.sendRedirect(req.getContextPath() + "/assets/html/meeting_add.jsp");
                return;
            }
            int cid = meetingService.addMeeting(title,deptName,startDate + " " + startTime,content,uid);
            if (cid > 0){
                meetingService.joinMeeting(cid,deptName);
                session.setAttribute("meetMsg","发布成功");
                resp.sendRedirect(req.getContextPath() + "/meeting/meetingList");
            }else{
                session.setAttribute("meetMsg","发布失败");
                resp.sendRedirect(req.getContextPath() + "/assets/html/meeting_add.jsp");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void meetDetail(HttpServletRequest req, HttpServletResponse resp){
        String cidStr = req.getParameter("cid");
        HttpSession session = req.getSession();
        int cid = 0;
        if(null == cidStr || "".equals(cidStr)){
            cidStr = "0";
        }
        cid = Integer.parseInt(cidStr);
        Map<String,Object> map = meetingService.meetDetail(cid);
        session.setAttribute("meetDetail",map);
        try {
            resp.sendRedirect(req.getContextPath() + "/assets/html/meeting_detail.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
